package com.dragon.modules.yuegou.service.impl.merchant;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.merchant.MerchantActivityCaseEntity;
import com.dragon.modules.yuegou.mapper.merchant.MerchantActivityCaseMapper;
import com.dragon.modules.yuegou.model.merchant.DescribeImage;
import com.dragon.modules.yuegou.model.merchant.MerchantActivityCase;
import com.dragon.modules.yuegou.query.merchant.MerchantActivityCaseQuery;
import com.dragon.modules.yuegou.service.merchant.MerchantActivityCaseService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 优秀案例 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-23 18:02:18
 * @version 2.0
 */
@Service("merMerchantActivityCaseService")
public class MerchantActivityCaseServiceImpl
		extends ServiceImpl<MerchantActivityCaseMapper, MerchantActivityCaseEntity>
		implements MerchantActivityCaseService {

	private static final String MAST_POSITIVE_INTEGER = "排序必须是正整数";

	private static final String MAST_POSITIVE_NUMBER = "排序必须是正数";

	private static final String ID_NOT_NULL = "案例Id不能为空";

	@Override
	public IPage<MerchantActivityCase> search(PageRequest page, MerchantActivityCaseQuery query) {
		query.setDel(false);
		return page(PageToResultUtils.resultToPage(page), query.toQueryWrapper());
	}

	@Override
	public void saveCase(MerchantActivityCase activityCase) {
		Check.notEmpty(activityCase.getCaseName(),"案例名称不能为空");
		
		if (activityCase.getViews() != null) {
			Check.digits(activityCase.getViews(),11,0,MAST_POSITIVE_INTEGER);
			Check.positive(activityCase.getViews(),MAST_POSITIVE_NUMBER);
		}
		
		if (activityCase.getShares() != null) {
			Check.digits(activityCase.getShares(),11,0,MAST_POSITIVE_INTEGER);
			Check.positive(activityCase.getShares(),MAST_POSITIVE_NUMBER);
		}
		
		List<DescribeImage> describeImages = new ArrayList<>();
		String image = activityCase.getDescribeImage();
		if(image.indexOf(",") == -1) {
			DescribeImage describeImage = new DescribeImage();
			describeImage.setImg(image);
			describeImages.add(describeImage);
			
		} else {
			for(int i=0;i<image.split(",").length;i++) {
				DescribeImage describeImage = new DescribeImage();
				describeImage.setImg(image.split(",")[i]);
				describeImages.add(describeImage);
			}
		}
		activityCase.setDescribeImage(JSON.toJSONString(describeImages));
		if (StringUtils.isEmpty(activityCase.getId())) {
			//添加商户案例
			save(activityCase);
		}else {
			updateById(activityCase);
		}
		
	}

	@Override
	public MerchantActivityCase getCase(String id) {
		return new MerchantActivityCase(getById(id));
	}

	@Override
	public void deleteCase(String id) {
		Check.notEmpty(id,ID_NOT_NULL);
		
		MerchantActivityCase activityCase = new MerchantActivityCase();
		activityCase.setId(id);
		activityCase.setDel(true);
		updateById(activityCase);
	}

	@Override
	public IPage<MerchantActivityCase> search(PageRequest page, String caseName, Boolean views,
			Boolean shares) {
		MerchantActivityCaseQuery query = new MerchantActivityCaseQuery();	
		if (StringUtils.isNotEmpty(caseName)) {
			query.caseName().like(caseName);
		}
		query.setDel(false);
		QueryWrapper<MerchantActivityCaseEntity> wrapper =  query.toQueryWrapper();
		if (views == null && shares == null ) {
			wrapper.orderByAsc(MerchantActivityCaseEntity.COL_SORT_NO);
			wrapper.orderByDesc(MerchantActivityCaseEntity.COL_CREATED_TIME);
		}
		
		if (views != null) {
			if (views) {
				wrapper.orderByAsc(MerchantActivityCaseEntity.COL_VIEWS);
			}else {
				wrapper.orderByDesc(MerchantActivityCaseEntity.COL_VIEWS);
			}
		}
		
		if (shares != null) {
			if (shares) {
				wrapper.orderByAsc(MerchantActivityCaseEntity.COL_SHARES);
			}else {
				wrapper.orderByDesc(MerchantActivityCaseEntity.COL_SHARES);
			}
		}

		return page(PageToResultUtils.resultToPage(page), wrapper);
	}

	@Override
	public MerchantActivityCase caseList(String id) {
		Check.notEmpty(id,ID_NOT_NULL);
		return getCase(id);
	}

	@Override
	public void saveSortNo(String id, Integer sortNo) {
		Check.notEmpty(id,ID_NOT_NULL);
		Check.digits(sortNo,5,0,MAST_POSITIVE_INTEGER);
		Check.positive(sortNo,MAST_POSITIVE_NUMBER);
		
		MerchantActivityCase merchantActivityCase = new MerchantActivityCase();
		merchantActivityCase.setId(id);
		merchantActivityCase.setSortNo(sortNo);
		updateById(merchantActivityCase);
	}

}